package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@Service(interfaceClass = SetMealService.class)
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> setmeals = setMealMapper.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setMealMapper.add(setmeal);
        Integer setMealId = setmeal.getId();
        if(checkgroupIds!=null && checkgroupIds.length>0){
            for (Integer checkGroupId : checkgroupIds) {
                Map map=new HashMap<>();
                map.put("setMealId",setMealId);
                map.put("checkGroupId",checkGroupId);
                setMealMapper.setSetMealAndCheckGroup(map);
            }
        }
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public List<Map<String, Object>> findSetMealCount() {
        return setMealMapper.findSetMealCount();
    }


}
